﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using HZ.Supervise.IBLL;
using HZ.Supervise.Model;
using System.Web.UI.DataVisualization.Charting;
using System.Drawing;
namespace HZ.Supervise.Web.Secondphase.ReportSort
{
    public partial class SortCurrentGoodsList : BasePage
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["query"] = URLUtil.GetSuperviseQueryFromURL(Request);
                SuperviseQuery sq = (SuperviseQuery)ViewState["query"];

                if (!string.IsNullOrEmpty(sq.Mid)) //如果月份为空 参数有误
                {
                    BindList();
                    GetStatistics();
                }
                else
                {
                    Response.Write("<script>history.go(-1);<script>");
                }
            }
        }
        /// <summary>
        /// 统计采购入库总金额
        /// </summary>
        void GetStatistics()
        {
            double purchaseAmountTotal = 0;//采购总金额
            double wareHouseAmountTotal = 0;//入库总金额
            for (int i = 0; i < gvwSortInfo.Rows.Count; i++)
            {
                purchaseAmountTotal += double.Parse(gvwSortInfo.Rows[i].Cells[3].Text);
                wareHouseAmountTotal += double.Parse(gvwSortInfo.Rows[i].Cells[5].Text);
            }
            this.lblCount.Text = "采购总金额为:" + purchaseAmountTotal.ToString() + ";入库总金额为:" + wareHouseAmountTotal.ToString(); ;
        }
        void BindList()
        {
            SuperviseQuery sq = (SuperviseQuery)ViewState["query"];
            string strMosth = sq.Mid;
            sq.Append = Function.FormatPlanTime("PlanTime", strMosth);
            sq.Mid = "";
            int t = 0;
            HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();
            ie.QueryObject = new HZ.Supervise.Model.QueryObject()
            {
                //分组字段
                GroupField = GroupByField.PurchaseTimeMonth | GroupByField.SortSource | GroupByField.SortID,
                //查询条件
                Query = sq,
                //分组查询字段
                GroupSelectField = GroupFields.PurchaseAmountTotal | GroupFields.WareHouseAmountTotal | GroupFields.WareHouseCountTotal | GroupFields.PurchaseCountTotal,
                //排序字段
                SortField = "PurchaseTimeMonth ASC"
            };

            //将数据填入gridview中
            System.Data.DataTable dt = ie.ExecutePage(1,0, out t);
            sq.Mid = strMosth;
            gvwSortInfo.DataSource = dt;
            gvwSortInfo.DataBind();
            for (int j = 0; j < gvwSortInfo.Rows.Count; j++)
            {
                //名称相同才能合并
                //防止超出索引
                if (j < (gvwSortInfo.Rows.Count - 1))
                {
                    if (gvwSortInfo.Rows[j].Cells[0].Text.Trim() == gvwSortInfo.Rows[j + 1].Cells[0].Text.Trim())
                    {
                        //合并头
                        gvwSortInfo.Rows[j].Cells[0].RowSpan = 2;
                        gvwSortInfo.Rows[j + 1].Cells[0].Visible = false;

                        //合并尾
                        gvwSortInfo.Rows[j].Cells[6].RowSpan = 2;
                        gvwSortInfo.Rows[j + 1].Cells[6].Visible = false;
                        j++;
                    }
                    else
                    {

                    }
                }

            }
        }
        protected void gvwSortInfo_RowCreated(object sender, GridViewRowEventArgs e)
        {
            #region 合并表头
            switch (e.Row.RowType)
            {
                case DataControlRowType.Header:
                    SuperviseQuery sq = (SuperviseQuery)ViewState["query"];
                    //取得下拉框的月份


                    //第一行表头
                    TableCellCollection tcHeader = e.Row.Cells;
                    tcHeader.Clear();
                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[0].Attributes.Add("colspan", "2"); //跨Row
                    tcHeader[0].Attributes.Add("bgcolor", "LightSteelBlue");
                    //tcHeader[0].Attributes.Add("bgcolor", "white");
                    tcHeader[0].Text = "";
                    //tcHeader.Add(new TableHeaderCell());
                    //tcHeader[1].Attributes.Add("bgcolor", "Red");
                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[1].Attributes.Add("colspan", "4"); //跨Column
                    tcHeader[1].Text = sq.Mid;

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[2].Attributes.Add("colspan", "2"); //跨Column
                    tcHeader[2].Text = "</th></tr><tr>";



                    //第2行表头
                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[3].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[3].Text = "类别";
                    
                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[4].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[4].Text = "是否基药";
                    
                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[5].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[5].Text = "采购总金额";
                    
                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[6].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[6].Text = "采购总数量";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[7].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[7].Text = "入库总金额";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[8].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[8].Text = "入库总数量";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[9].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[9].Text = "操作</th></tr><tr>";
                    break;
            }
            #endregion
        }

        protected void gvwSortInfo_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "DirectByDetail")
            {
                string sessionid = Guid.NewGuid().ToString();
                SuperviseQuery query = (SuperviseQuery)ViewState["query"];
                try
                {
                    query.SortName = e.CommandArgument.ToString();
                }
                catch { }
                Session[sessionid] = query;
                string url = "SortGoodsList.aspx";
                Response.Redirect(string.Format("{0}?sessionid={1}", url, sessionid));
            }
        }
    }
}
